package com.cskaoyan.gateway.controller.user;

import com.cskaoyan.gateway.form.login.LoginForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IMemberService;
import com.mall.user.IUserService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Liang Shunhao
 * @description: 用户登录
 * @date 2021/9/28 20:29
 */
@RestController
@RequestMapping("user")
public class LoginController {

    @Reference(timeout = 3000, check = false)
    IUserService userService;

    @Reference(timeout = 3000, check = false)
    IKaptchaService kaptchaService;

    /**
     * 用户登录模块
     *
     * @return 登录验证信息
     */
    @PostMapping("login")
    @Anoymous
    public ResponseData login(@RequestBody LoginForm loginForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        // 验证码判断
        KaptchaCodeRequest request = new KaptchaCodeRequest();
        String code = loginForm.getCaptcha();
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        request.setUuid(uuid);
        request.setCode(code);
        KaptchaCodeResponse response = kaptchaService.validateKaptchaCode(request);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            // 验证码错误
            return new ResponseUtil<>().setErrorMsg(response.getCode());
        }

        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(loginForm.getUserName());
        userLoginRequest.setPassword(loginForm.getUserPwd());
        // 验证登录信息
        UserLoginResponse userLoginResponse = userService.checkLogin(userLoginRequest);

        if (userLoginResponse == null) {
            return new ResponseUtil<>().setErrorMsg("用户名或密码错误");
        }

        // 将token保存到Cookie
        Cookie cookie = new Cookie("access_token", userLoginResponse.getToken());
        CookieUtil.setCookie(httpServletResponse, cookie);

        return new ResponseUtil<>().setData(userLoginResponse);
    }

    /**
     * 用户登录模块
     *
     * @return 登录验证信息
     */
    @Anoymous
    @GetMapping("login")
    public ResponseData getLogin(HttpServletRequest request) {
        String userInfo = CookieUtil.getCookieValue(request, "userInfo");

        AuthResponse authResponse = new AuthResponse();
        authResponse.setUid(0);
        authResponse.setUsername(userInfo);
        return new ResponseUtil<>().setData(authResponse);
    }

    /**
     * 用户登出
     *
     * @return 登出信息
     */
    @RequestMapping("loginOut")
    public ResponseData loginOut(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            cookie.setMaxAge(0);
        }

        return new ResponseUtil<>().setData("退出成功");
    }
}
